package com.emm.yixun.website.controller.workflow.online.myTask;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.emm.yixun.common.model.enums.AuditStatusEnum;
import com.emm.yixun.website.service.ActivitiProcessServiceImpl;
import com.emm.yixun.website.vo.process.ProcessScheduleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.emm.yixun.common.model.MearchantProcdef;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.ActNodeDataDto;
import com.emm.yixun.common.model.dto.ActNodeQuery;
import com.emm.yixun.common.model.dto.MearchantProcdefDto;
import com.emm.yixun.common.model.dto.ProjectDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.page.PageUtil;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.estate.service.ActNodeDataServiceFacade;
import com.emm.yixun.estate.service.ProjectServiceFacade;
import com.emm.yixun.system.service.MearchantProcdefServiceFacade;
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.mybatis.Page;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;

/**
 * 我的任务
 */
@Controller
@RequestMapping(value = "/backstage/workflow/online/myTask/")
public class MyTaskController extends BaseController<Object> {
	@Autowired
	private ActNodeDataServiceFacade actNodeDataServiceFacade;
	@Autowired
	private ProjectServiceFacade projectService;
	
	@Autowired
	private MearchantProcdefServiceFacade mearchantProcdefServiceFacade;

	@Autowired
	private ActivitiProcessServiceImpl activitiProcessServiceImpl;

	/**
	 * 
	 * toMyHandling:(待办，已办，发起通用页面). <br/>
	 *
	 * @author Administrator
	 * @param model
	 * @param qType
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "/toMyHandling/{qType}")
	public String toMyHandling(org.springframework.ui.Model model,@PathVariable("qType") String qType) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			User user = ShiroUtil.getCurrentUser();
			MearchantProcdefDto procdefDto = new MearchantProcdefDto();
			procdefDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			procdefDto.setMerchantId(user.getMerchantId());
			ResponseList<MearchantProcdef> mearchantList = mearchantProcdefServiceFacade.findByDto(procdefDto);
			if(mearchantList.isSuccess()){
				model.addAttribute("processTypeEnums", mearchantList.getEntity());
			}

			ProjectDto projectDto = new ProjectDto();
			projectDto.setMerchantId(user.getMerchantId());
			projectDto.setPermissionUserId(user.getId());
			projectDto.setResourcesCode("menu_0006_0020");
			model.addAttribute("projectList",projectService.findByDto(projectDto).getEntity());
			//model.addAttribute("permitBtn",getPermitBtn(Const.RESOURCES_TYPE_FUNCTION));
			//判断是否拥有项目列表【全部】权限
			model.addAttribute("permitBtn", doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "myTask:list:all")); 
			return "/system/workflow/online/myTask/handlingListLC";
		}
		return Const.NO_AUTHORIZED_URL;
	}
	/**
	 * 
	 * findMyHandling:(流程通用查询). <br/>
	 *
	 * @author Administrator
	 * @param page
	 * @param actNodeQuery
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "findMyHandlingLC", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes findMyHandling(Page<ActNodeDataDto> page,ActNodeQuery actNodeQuery) {
		AjaxRes ar = getAjaxRes();
		int pageNum = PageUtil.begin(page.getPageNum(), page.getPageSize());
		int pageSize = page.getPageSize();
		User user = ShiroUtil.getCurrentUser();
		com.emm.yixun.common.page.Page<ActNodeDataDto> responseList = actNodeDataServiceFacade.queryActHaveList(actNodeQuery, user, pageNum, pageSize);
		if (responseList.getSuccess()) {
			long count = responseList.getTotal();
			page.setTotalRecord((int) count);
			if(responseList.getRows()!=null && responseList.getRows().size()>0){
				for(ActNodeDataDto actNodeDataDto : responseList.getRows()) {
					String auditStatus = "";
					switch (actNodeDataDto.getAuditStatus()){
						case "":
							auditStatus = "审批中";
							break;
						case "0":
							auditStatus = "已通过";
							break;
						case "1":
							auditStatus = "已驳回";
							break;
					}
					actNodeDataDto.setAuditStatus(auditStatus);
				}
			}
			page.setResults(responseList.getRows());
			Map<String, Object> p = new HashMap<String, Object>();
			p.put("list", page);
			ar.setSucceed(p);
			ar.setRes(Const.SUCCEED);
		} else {
			ar.setRes(Const.FAIL);
		}
		return ar;
	}
}
